package com.asom.player.service;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Set;
import java.util.TreeSet;

import android.content.Context;

public class Favorites
{
	Context context;
	
	Set<Song>			songs;
	
	@SuppressWarnings("unchecked")
	void load()
	{		
		FileInputStream 	fis = null;
		ObjectInputStream	ois = null;
		
		try
		{
			fis = context.openFileInput("Favorites");
			ois = new ObjectInputStream(fis);
		
			songs = (TreeSet<Song>) ois.readObject();
		}
		catch (Exception e)
		{
			songs = new TreeSet<Song>();
		}
	}
	
	public void save()
	{
		FileOutputStream	fos = null;
		ObjectOutputStream	oos = null;
		
		try
		{
			fos = context.openFileOutput("Favorites", Context.MODE_PRIVATE);
			oos = new ObjectOutputStream(fos);
			
			oos.writeObject(songs);
		}
		catch (FileNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public Favorites()
	{
		
	}
	
	public void finalize()
	{
		this.save();
	}
	
	public void setContext(Context _context)
	{
		context = _context;
		
		this.load();
	}
	
	public void addSong(Song _song)
	{
		songs.add(_song);
	}
	
	public void removeSong(Song _song)
	{
		songs.remove(_song);
	}
	
	Song[] getSongs()
	{
		return songs.toArray(new Song[songs.size()]);
	}
}
